<template>
  <div>
    <s-table
      ref="table"
      size="default"
      rowKey="key"
      :columns="columns"
      :data="loadData"
      showPagination="auto"
    >
      <span slot="baseinfo" slot-scope="text, record">
        <img style="width: 45px; heigth: 45px;border-radius:5px;margin-right:10px" :src="record.avatar_url" />
        <ellipsis :length="20" tooltip>{{ record.nickname }} </ellipsis>
      </span>
      <span slot="platform" slot-scope="text">
        <ellipsis v-show="text == '0'" :length="4" tooltip>微信</ellipsis>
        <ellipsis v-show="text == '1'" :length="4" tooltip>支付宝</ellipsis>
        <ellipsis v-show="text == '3'" :length="4" tooltip>手机</ellipsis>
      </span>
      <span slot="time" slot-scope="text, record">
        <span>{{ record.created_at | timeFormat }}</span>
      </span>
      <span slot="action" slot-scope="text, record">
        <template>
          <a-button type="link" @click="delAdmin(record)" size="small">删除</a-button>
        </template>
      </span>
    </s-table>
    <a-modal
      title="编辑用户"
      :width="800"
      :mask-closable="false"
      :destroy-on-close="true"
      v-model="visible"
      :footer="null"
    >
      <a-card :bordered="false" class="card_top_padding" style="margin-bottom: 11px">
        <div class="table-page-search-wrapper">
          <a-form layout="inline">
            <a-row :gutter="20" style="padding-top: 20px;">
              <a-col :md="8" :sm="24">
                <a-form-item>
                  <a-input-search v-model="queryParamSelect.name" placeholder="请输入用户昵称/名称" />
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-item>
                  <a-input-search v-model="queryParamSelect.phone" placeholder="请输入用户手机号" />
                </a-form-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <span class="table-page-search-submitButtons" style="margin-bottom:0">
                  <a-button style="margin-right: 8px" @click="() => (this.queryParamSelect = {})">重置</a-button>
                  <a-button type="primary" @click="onPageSearch">查询</a-button>
                </span>
              </a-col>
            </a-row>
          </a-form>
        </div>
      </a-card>
      <s-table
        ref="table1"
        size="default"
        rowKey="key"
        :columns="columns"
        :data="loadDataSelect"
        showPagination="auto"
      >
        <span slot="baseinfo" slot-scope="text, record1">
          <img style="width: 45px; heigth: 45px;border-radius:5px;margin-right:10px" :src="record1.avatar_url" />
          <ellipsis :length="20" tooltip>{{ record1.nickname }} </ellipsis>
        </span>
        <span slot="platform" slot-scope="text">
          <ellipsis v-show="text == '0'" :length="4" tooltip>微信</ellipsis>
          <ellipsis v-show="text == '1'" :length="4" tooltip>支付宝</ellipsis>
          <ellipsis v-show="text == '3'" :length="4" tooltip>手机</ellipsis>
        </span>
        <span slot="time" slot-scope="text, record1">
          <span>{{ record1.created_at | timeFormat }}</span>
        </span>
        <span slot="action" slot-scope="text, record1">
          <a-button type="primary" @click="addAdmin(record1)" size="small">添加</a-button>
        </span>
      </s-table>
    </a-modal>
  </div>
</template>

<script>
import SelectUser from '@/components/SelectUser/SelectUser'
import zhCN from 'ant-design-vue/lib/locale-provider/zh_CN'
import moment from 'moment'
import { STable, Ellipsis } from '@/components'
import { getAdminUser, addAdmin, delAdmin } from '@/api/user'
const columns = [
  {
    title: 'ID',
    dataIndex: 'id'
  },
  {
    title: '用户信息',
    dataIndex: 'nickname',
    scopedSlots: { customRender: 'baseinfo' },
    width: 200
  },
  {
    title: '会员等级',
    dataIndex: 'l_name'
  },
  {
    title: '绑定手机号',
    dataIndex: 'binding'
  },
  {
    title: '操作',
    dataIndex: 'action',
    width: '150px',
    scopedSlots: { customRender: 'action' }
  }
]

export default {
  name: 'UserList',
  components: {
    STable,
    Ellipsis,
    SelectUser
  },
  props: {
    queryParam: {
      type: Object,
      default: () => {}
    }
  },
  data () {
    this.columns = columns
    return {
      locale: zhCN,
      user_id: 0,
      // 会员列表
      roleList: [],
      visible: false,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      queryParamSelect: {},
      loadData: (parameter) => {
        Object.assign(parameter, this.queryParam)
        parameter.is_admin = 1
        return getAdminUser(parameter).then((res) => {
          this.roleList = res.data.level
          return res.data
        })
      },
      loadDataSelect: (parameter) => {
        Object.assign(parameter, this.queryParamSelect)
        parameter.is_admin = 0
        return getAdminUser(parameter).then((res) => {
          this.roleList = res.data.level
          return res.data
        })
      }
    }
  },
  filters: {},
  computed: {},
  methods: {
    toggleAdvanced () {
      this.advanced = !this.advanced
    },
    resetSearchForm () {
      this.queryParam = {
        dateStart: moment(new Date()),
        dateEnd: moment(new Date())
      }
    },
    moment,
    showAdd () {
      this.visible = true
    },
    addAdmin (user) {
      var params = {
        id: user.id
      }
      const hide = this.$message.loading('操作中...', 0)
      addAdmin(params).then((res) => {
        hide()
        if (res.code !== 0) {
          this.$error({
            content: res.msg
          })
        } else {
          this.onPageSearch()
        }
      })
    },
    delAdmin (user) {
      var params = {
        id: user.id
      }
      const hide = this.$message.loading('操作中...', 0)
      delAdmin(params).then((res) => {
        hide()
        if (res.code !== 0) {
          this.$error({
            content: res.msg
          })
        } else {
          this.$nextTick(() => {
            this.$refs.table.refresh(true)
          })
        }
      })
    },
    onPageSearch () {
      this.$nextTick(() => {
        this.$refs.table1.refresh()
        this.$refs.table.refresh(true)
      })
    }
  }
}
</script>
